/**
 * Author:
 * Date:
 * Description: ""
 */
require("../common/common.scss");
require("./tuoguan.scss");

var loading = require('COMMON/js/util.loading.pc.js');

var PaginationX = require("COMMON/modules/pagination-x/v1.0");

var ParseTemplate = ZFT.Util.ParseTemplate;
var Template = {
    tr: ParseTemplate(require("./tpl/tr.xtpl")),
    billDetail: ParseTemplate(require("./tpl/detail.xtpl"))
};

var Main = ZFT.Util.Class({
    PAGE_SIZE: 15,

    selectedContract: [],

    FEE_TYPE: [
        '房租',
        '物业',
        '水',
        '电'
    ],

    dom: {
        filter: '#filter',
        beginDate: '#beginDate',
        endDate: '#endDate',
        owner: '#houseOwner',
        phone: '#searchPhone',
        state: '#state',
        btnQuery: '#btnQuery',
        pagination: $(".paginationWrap"),
        tbBill: '#tbBill'
    },

    xhrGetBillList: null,

    init: function () {
        var _this = this;

        var dom = _this.dom;

        this.params = {
            begin_month: '',
            end_month: '',
            name: '',
            mobile: '',
            status: '0',
            page: 1,
            page_size: this.PAGE_SIZE
        };

        this.ajaxGetBillList(this.params);

        $('#beginDate').datepicker({
            format: "yyyymm",
            startView: 1,
            minViewMode: 1,
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });

        $('#endDate').datepicker({
            format: "yyyymm",
            startView: 1,
            minViewMode: 1,
            maxViewMode: 2,
            language: "zh-CN",
            autoclose: true
        });

        // $( this.dom.tbBill ).on('click', '.btn-getdetail', function( e ){

        //     var id = $(e.target).attr('data-id'),
        //         status = $(e.target).attr('data-status');

        //     _this.ajaxGetBillDetail( id, status );

        // });

        $(this.dom.btnQuery).on('click', function () {
            var beginDate = $.trim($(_this.dom.beginDate).val()),
                endDate = $.trim($(_this.dom.endDate).val());
            var page_size = $(".paginationWrap").attr("data-size");
            if (beginDate > endDate) {
                ZFT.Util.STip('fail', '起始时间不能大于结束时间');
                return false;
            }

            _this.params = {
                begin_month: beginDate,
                end_month: endDate,
                name: $(_this.dom.owner).val(),
                mobile: $(_this.dom.phone).val(),
                status: $(_this.dom.state).val(),
                page: 1,
                page_size: page_size ? page_size : _this.PAGE_SIZE
            };

            _this.ajaxGetBillList(_this.params);
        });

    },

    ajaxGetBillList: function (params) {
        var _this = this;

        this.xhrGetBillList = this.ajaxRenderData({
            container: this.dom.tbBill + ' tbody',
            url: '/r/BillManager_trustPayBill',
            params: params,
            loading: {
                id: 'billLoading',
                text: '正在努力加载您的账单...',
                className: 'tb-loading',
                colspan: 12,
                tag: 'td'
            },
            success: function (res) {
                _this.renderBillList(res.data.list);

                if (_this.pagination) {

                    _this.pagination.render({ current: res.data.current_page, total: res.data.total });

                } else {

                    _this.pagination = new PaginationX({
                        container: $(".paginationWrap"),
                        count: 7,
                        showTotal: true,
                        jump: true,
                        filter: true
                    });

                    _this.pagination.on("page.switch", function (toPage, current, total) {
                        var page_size = $(".paginationWrap").attr("data-size");
                        _this.params.page = toPage;
                        _this.params.page_size = page_size ? page_size : 15;
                        _this.ajaxGetBillList(_this.params);

                    });

                    _this.pagination.render({ current: res.data.current_page, total: res.data.total });

                }
            },
            error: function (res) {
                $(_this.dom.tbBill + ' tbody').html('<tr><td colspan="12" align="center" class="tb-nodata">' + res.msg + '</td></tr>');
            }
        })
    },
    ajaxRenderData: function (opts) {
        return ZFT.Util.Ajax(opts.url, {
            type: 'POST',
            params: opts.params,
            loading: function () {
                var loadingOpts = {};

                loadingOpts.id = opts.loading.id;
                loadingOpts.text = opts.loading.text || '';
                loadingOpts.className = opts.loading.className;
                loadingOpts.tag = opts.loading.tag || '';
                if (opts.loading.tag == 'td') {
                    loadingOpts.colspan = opts.loading.colspan ? opts.loading.colspan : '';
                }

                var html = loading(opts.loading.text, loadingOpts);
                $(opts.container).html(html);
            },
            success: function (res) {
                $('#' + opts.loading.id).remove();


                if (res.code == 200 || res.code == 'success') {
                    opts.success && opts.success(res);
                } else {
                    // ZFT.Util.STip( res.msg );
                    opts.error && opts.error(res);
                }
            },
            serverError: function (xhr, txt) {
                // ZFT.Util.STip( txt );
            }
        })
    },

    renderBillList: function (data) {
        var html = Template.tr({ data: data });
        $(this.dom.tbBill + ' tbody').html(html);
    }


});

$(function () {
    new Main();
})